
#include "../include/chat_service_rpc_server.h"
#include "../include/user_list_manger.h"
#include <nlohmann/json.hpp>
#include "../include/global.h"
#include "../include/chat_message_in_memory.h"

::grpc::Status chat_service_rpc_server::notifyChatTextMsg(::grpc::ServerContext* context, const ::message::reqNotifyChatTextMsg* request, ::message::resNotifyChatTextMsg* response){
    //转发处理
    const std::string & to_email=request->to_email();
    //去本地找
    auto tcp_connection=user_list_manger::get_instance()->find_and_get_con(to_email);
    if(tcp_connection==nullptr){
        std::cout<<"NotifyChatTextMsg:需要转发的email:"<<to_email<<"居然不在预期的服务器里"<<std::endl;
        response->set_result(false);
        return grpc::Status::OK;
    }
    //如果存在，那么构造json进行发送
    nlohmann::json res_json;
    res_json["from_email"]=request->from_email();
    res_json["to_email"]=to_email;
    res_json["msg_type"]=request->msg_type();
    res_json["msg_id"]=request->msg_id();
    //需要根据类型进行不同的处理
    if(request->msg_type()==chatType::PLAIN_TEXT){
        res_json["msg_data"]=request->msg_data();
    }else if(request->msg_type()==chatType::file){
        const std::string& file_str=request->msg_data();
        nlohmann::json file_json=nlohmann::json::parse(file_str);
        res_json["msg_data"]=file_json;
    }
    
   
    std::string data=res_json.dump();

    tcp_connection->async_write_msg(response_package_id::NOTIFY_CHAT_TEXT_MSG,data.size(),data.c_str());
    //然后保存到内存上
    message_item *item=new message_item(request->from_email(),to_email,request->msg_type(),request->msg_id(),request->msg_data());
    message_list::get_instance()->insert_message_item(request->msg_id(),item);
    response->set_result(true);
    return grpc::Status::OK;



    
}

::grpc::Status chat_service_rpc_server::NotifyAddFriendApply(::grpc::ServerContext* context, const ::message::reqNotifyAddFriendApply* request, ::message::resNotifyAddFriendApply* response){
    //
    const std::string & to_email=request->to_email();
    //在本服务器中找，没找到直接返回即可
    auto tcp_connection=user_list_manger::get_instance()->find_and_get_con(to_email);
    if(tcp_connection==nullptr){
        std::cout<<"NotifyAddFriendApply:需要转发的email:"<<to_email<<"居然不在希望的服务器里"<<std::endl;
        response->set_result(false);
        return grpc::Status::OK;
    }

    //如果存在，那么构造json进行发送
    nlohmann::json res_json;
    res_json["from_email"]=request->from_email();
    res_json["to_email"]=to_email;
    res_json["from_name"]=request->from_name();
    res_json["varify_text"]=request->varify_text();
    std::string data=res_json.dump();

    tcp_connection->async_write_msg(response_package_id::RESPONSE_ADD_FRIEND_NOTIFY,data.size(),data.c_str());

    return grpc::Status::OK;

}


::grpc::Status chat_service_rpc_server::NotifyFriendApplyRes(::grpc::ServerContext* context, const ::message::reqNotifyFriendApplyRes* request, ::message::resNotifyFriendApplyRes* response){
    const std::string& need_send_tar_email=request->need_send_tar_email();
    //在本服务器中找，没找到直接返回即可
    auto tcp_connection=user_list_manger::get_instance()->find_and_get_con(need_send_tar_email);
    if(tcp_connection==nullptr){
        std::cout<<"NotifyAddFriendApply:需要转发的email:"<<need_send_tar_email<<"居然不在希望的服务器里"<<std::endl;
        response->set_result(false);
        return grpc::Status::OK;
    }
    //存在，构造req_json并发送
    nlohmann::json res_json;
    res_json["to_email"]=request->to_email();
    res_json["apply_res"]=request->apply_res();
    std::string data=res_json.dump();

    tcp_connection->async_write_msg(response_package_id::RESPONSE_ADD_FRIEND_NOTIFY,data.size(),data.c_str());
    response->set_result(true);
    return grpc::Status::OK;

}